package com.ht.medicaltreatment.fragment

import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ItemListAdapter
import com.ht.medicaltreatment.bean.ItemListBean
import com.ht.medicaltreatment.viewmodel.ItemFragmentViewModel
import kotlinx.android.synthetic.main.fragment_item_layout.*

class ItemFragment : BaseFragment() {

    private var mModel : ItemFragmentViewModel ? = null
    private var type = 0

    override fun getLayoutResId() = R.layout.fragment_item_layout

    override fun init() {
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
    }

    fun initData(type:Int,checkId:Int,id: Int,onClick:(String,Int)->Unit) {
        this.type = type
        if(mModel == null){
            mModel = ItemFragmentViewModel()
        }
        when(type){
            0 -> {
                //冰箱位置
                mModel?.fridge{
                    setData(checkId, it, onClick)
                }
            }
            1 -> {
                //层数
                mModel?.floor(id){
                    setData(checkId, it, onClick)
                }
            }
            2 -> {
                //抽屉
                mModel?.drawer(id){
                    setData(checkId, it, onClick)
                }
            }
            3 -> {
                //位置
                mModel?.location(id){
                    setData(checkId, it, onClick)
                }
            }

        }
    }

    private fun setData(
        checkId: Int,
        it: MutableList<ItemListBean>,
        onClick: (String, Int) -> Unit
    ) {
        recyclerView.adapter = ItemListAdapter(checkId, it)
            .apply {
                setOnItemClickListener { adapter, view, position ->
                    val item = adapter.getItem(position) as ItemListBean
                    if(TextUtils.isEmpty(item.disable)){
                        onClick(item.name, item.id)
                        (adapter as ItemListAdapter).setCheckId(item.id)
                        adapter.notifyDataSetChanged()
                    }
                }
            }
    }
}